<?php
//hktun_paypal
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');
require_once(dirname(dirname(dirname(__FILE__))) .'/include/library/payment/Paypal.php'); //get from paypal library

$_input_charset = 'utf-8';
$partner = $INI['paypal']['mid'];
$security_code = $INI['paypal']['sec'];
$sign_type = 'MD5';
$transport = 'http';

//echo "passing variable test..";

$myPaypal= new Paypal();

// Log the IPN results
$myPaypal->ipnLog = TRUE;

//echo "reach here";
// Enable test mode if needed
//hktun_paypal_testmode
//$myPaypal->enableTestMode();


// Check validity and write down it
if ($myPaypal->validateIpn())
{
	//example parameters
	//mc_gross=1.00, protection_eligibility=Ineligible, address_status=confirmed, payer_id=A5KBU5HMJW4QY, tax=0.00, address_street=1 Maire-Victorin, payment_date=01:38:39 Jun 28, 2010 PDT, payment_status=Pending, charset=windows-1252, address_zip=M5A 1E1, first_name=Test, mc_fee=0.33, address_country_code=CA, address_name=Test User, notify_version=3.0, custom=muri-khao, payer_status=verified, business=seller_1276523540_biz@gmail.com, address_country=Canada, address_city=Toronto, quantity=1, verify_sign=AFcWxV21C7fd0v3bYYYRCpSSRl31AExHfw3bRmKBjHYllYL7Rg5xHOwT, payer_email=buyer_1276523479_per@gmail.com, txn_id=0SH60371BS4669159, payment_type=instant, last_name=User, address_state=Ontario, receiver_email=seller_1276523540_biz@gmail.com, payment_fee=, receiver_id=GAM5AN6KJFW7S, pending_reason=paymentreview, txn_type=web_accept, item_name=T-Shirt, mc_currency=CAD, item_number=001, residence_country=CA, test_ipn=1, handling_amount=0.00, transaction_subject=muri-khao, payment_gross=, shipping=0.00, 

	
	$out_trade_no = $myPaypal->ipnData['custom']; 
	$total_fee = $myPaypal->ipnData['mc_gross'];
	$payment_status = $myPaypal->ipnData['payment_status'];// Pending - Completed - Denied - Refunded
	$currency = $myPaypal->ipnData['mc_currency'];
	
	//eric
	echo "out_trade_no=" . $out_trade_no . "total_fee=" . $total_fee;
	
	@list($_, $order_id, $city_id, $_) = explode('-', $out_trade_no, 4);
	
	if ( $_ == 'charge' ) {
		//hktun_paypal not sure this yet || $payment_status == 'Pending'
		if($payment_status == 'Completed' ) {  
			@list($_, $user_id, $create_time, $_) = explode('-', $out_trade_no, 4);
			ZFlow::CreateFromCharge($total_fee, $user_id, $create_time, 'paypal');
		}
	}
	// Refunded 
	else if( $payment_status == 'Refunded') {
	// do not refund money from paypal here
	// manually refund team buy money to user's account only
	}
	die('success');
}
    
die("fail");

?>

